package com.lightsh.flashlight;

import com.lightsh.light.Light;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

public class LightSwitchWidget extends AppWidgetProvider{
	private static final boolean DEBUG = false;
	private static final String TAG = "LightSwitchWidget";
	
	private static final String PREFERENCE_NAME = "LightData";
	private static final String PREFERENCE_KEY = "cur";
	
	public static void updateAllViews(Context context){
		AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
		int[] widgetIds = widgetManager.getAppWidgetIds(new ComponentName(context, LightSwitchWidget.class));
		updateViews(context, widgetIds);
	}
	
	private static void updateViews(Context context, int[] appWidgetIds){
		AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
		RemoteViews remoteView = null;
		
		for(int id : appWidgetIds){
			remoteView = getFitRemoteView(context);
			widgetManager.updateAppWidget(id, remoteView);
		}
	}
	
	private static RemoteViews getFitRemoteView(Context context){
		RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_main);
		int iconResource = R.drawable.ic_light_off;
		
		boolean bOpen = Light.isOpen();
		if(bOpen){
			iconResource = R.drawable.ic_light_on_c;
			switch(getCur(context)){
			case 0:
				iconResource = R.drawable.ic_light_on_a;
				break;
			case 1:
				iconResource = R.drawable.ic_light_on_b;
				break;
			case 2:
				iconResource = R.drawable.ic_light_on_c;
				break;
			case 3:
				iconResource = R.drawable.ic_light_on_d;
				break;
			case 4:
				iconResource = R.drawable.ic_light_on_e;
				break;
			}
		}
		
		remoteView.setImageViewResource(R.id.lightIcon, iconResource);		
		PendingIntent intent = PendingIntent.getBroadcast(context, 0, new Intent(Switcher.BROADCAST_NAME).setPackage(context.getPackageName()), 0);
		remoteView.setOnClickPendingIntent(R.id.LeftBtn, intent);
		
		if(bOpen){
			remoteView.setViewVisibility(R.id.brightnessLayout, View.VISIBLE);
			remoteView.setImageViewResource(R.id.brightnessIcon, R.drawable.ic_setting);
			Intent brightnessIntent = new Intent(Switcher.BROADCAST_NAME).setPackage(context.getPackageName());
			brightnessIntent.putExtra(Switcher.EXTRA_BRIGHTNESS, true);		
			brightnessIntent.addCategory(Switcher.EXTRA_BRIGHTNESS);
			intent = PendingIntent.getBroadcast(context, 0, brightnessIntent, 0);
			remoteView.setOnClickPendingIntent(R.id.RightBtn, intent);
		}else{
			remoteView.setViewVisibility(R.id.brightnessLayout, View.INVISIBLE);
		}
		return remoteView;
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		if(DEBUG){
			String logTip = "onUpdate:";
			for(int i = 0; i < appWidgetIds.length; ++i){
				if(i == 0){
					logTip += "[";
				}
				logTip += appWidgetIds[i];
				if(i != appWidgetIds.length - 1){
					logTip += ",";
				}else{
					logTip += "]";
				}
			}
			Log.e(TAG, logTip);
		}
		
		updateViews(context, appWidgetIds);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		if(DEBUG){
			String logTip = "onDeleted:";
			for (int i = 0; i < appWidgetIds.length; ++i) {
				if (i == 0) {
					logTip += "[";
				}
				logTip += appWidgetIds[i];
				if (i != appWidgetIds.length - 1) {
					logTip += ",";
				} else {
					logTip += "]";
				}
			}
			Log.e(TAG, logTip);
		}
		
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onDisabled(Context context) {
		if(DEBUG){
			Log.e(TAG, "onDisabled");
		}		
		super.onDisabled(context);
	}

	@Override
	public void onEnabled(Context context) {
		if (DEBUG) {
			Log.e(TAG, "onEnabled");
		}
		
		super.onEnabled(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if(DEBUG){
			Log.e(TAG, "onReceive:" + intent.getAction());
		}		
		super.onReceive(context, intent);
	}
	
	public static int getCur(Context context){
		int cur = 2;
		SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, 0);
		if(sp != null){
			cur = sp.getInt(PREFERENCE_KEY, cur);
		}
		return cur;
	}
	/**
	 * value :  0 - {@link Light#MAX_BRIGHTNESS_LEVEL}
	 * @param cur
	 */
	public static void saveCur(Context context, int cur){
		if(cur < 0 || cur > Light.MAX_BRIGHTNESS_LEVEL){
			throw new IllegalArgumentException("cur is not legal!");
		}
		SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, 0);
		if(sp != null){
			SharedPreferences.Editor editor = sp.edit();
			editor.putInt(PREFERENCE_KEY, cur);
			editor.commit();
		}
	}
}
